package com.dr.controller.index;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dr.entity.Address;
import com.dr.entity.Orderdetail;
import com.dr.entity.Orderinfo;
import com.dr.entity.Shopcart;
import com.dr.entity.User;
import com.dr.service.AddressService;
import com.dr.service.CommentWebService;
import com.dr.service.OrderinfoService;
import com.dr.service.ShopcartService;
import com.dr.util.LayUiResult;

@Controller
@RequestMapping("/index/")
public class OrderinfoWebController {
	@Autowired
	private OrderinfoService orderinfoService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private ShopcartService shopcarService;
	@Autowired
	private CommentWebService commentWebService;
    @RequestMapping("/toOrder.do")
	   public String toOrder(String idStr,HttpSession session,Model model) {
	  //获取当前登录的用户
	   User user = (User) session.getAttribute("loginUser");
	   session.setAttribute("shopCartIdSte", idStr);
	 //根据购物车id和用户id进行查询
	   List<Shopcart> orderList=orderinfoService.findShopByIds(idStr,user.getId());
	 //计算总金额
	   double orderMoney=0;
	   for(Shopcart s:orderList) {
		   orderMoney+=s.getTotalSum();
	   }
	   //查询当前登录用户的登录地址
	   List<Address> addresslist=addressService.findAddress(user.getId());
	   //保存数据
	   model.addAttribute("orderList",orderList);
	   model.addAttribute("addresslist",addresslist);
	   model.addAttribute("orderMoney",orderMoney);
	   return "order.jsp";
	   }
    /**
	 * " 下订单
	 * 
	 * @param addressId
	 * @param session
	 * @return
	 */
	@RequestMapping("/createOrder.do")
	@ResponseBody
	public Map<String, Object> toOrder(Integer addressId, HttpSession session) {
		// 获取当前用户
		User user = (User) session.getAttribute("loginUser");
		String shopCartIdStr = (String) session.getAttribute("shopCartIdSte");
		Address address = addressService.AllAddress(addressId);
		// 根据idstr字符串从购物车里查出对应的产品
		List<Shopcart> orderList = orderinfoService.findShopByIds(shopCartIdStr, user.getId());
		// 生产订单号
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String now = sdf.format(new Date());
		String orderId = "69" + now + "00";
		// 插入订单呢表
		Orderinfo orderinfo = new Orderinfo();
		orderinfo.setOrderId(orderId);
		orderinfo.setUser_id(user.getId());
		double money = 0;
		for (Shopcart s : orderList) {
			money += s.getTotalSum();
		}
		orderinfo.setMoney(money);// 总金额
		orderinfo.setStatus(0);
		orderinfo.setName(address.getName());
		orderinfo.setPhone(address.getPhone());
		orderinfo.setProvince(address.getProvince());
		orderinfo.setCity(address.getCity());
		orderinfo.setArea(address.getArea());
		orderinfo.setAddressInfo(address.getAddressInfo());
		orderinfoService.insertOrderinfo(orderinfo);
		// 插入订单民明细表
		for (Shopcart s : orderList) {
			// 循环执行SQL
			Orderdetail orderdetail = new Orderdetail();
			orderdetail.setPro_id(s.getPro_id());
			orderdetail.setOrd_orderId(orderId);
			orderdetail.setpName(s.getName());
			orderdetail.setPrice(s.getPrice());
			orderdetail.setCount(s.getCount());
			orderdetail.setSize(s.getSize());
			orderinfoService.insertOrderdetail(orderdetail);
		}
		// 删除购物车对应的产品
		for (Shopcart s : orderList) {
			shopcarService.deleteShopcart(s.getId() + "");
		}
		Map<String, String> map = new HashMap();
		map.put("orderId", orderId);
		map.put("orderMoney", money + "");
		map.put("orderUser", user.getName());
		return LayUiResult.toClient("0", "下单成功", 0, map);
	}
	//我的订单
	@RequestMapping("/toMyOrder.do")
	public String toMyOrder(Integer status,String orderId,HttpSession session,Model model) {
		User user = (User) session.getAttribute("loginUser");
		List<Orderinfo> list=orderinfoService.findOrderinfo(user.getId(), status, orderId);
		model.addAttribute("list",list);
		model.addAttribute("status",status);
		return "myorderq.jsp";
	}
	
	@RequestMapping("/toMygxin.do")
	public String toMygxin(Integer status,String orderId,HttpSession session,Model model) {
		User user = (User) session.getAttribute("loginUser");
		List<Orderinfo> list=orderinfoService.findOrderinfo(user.getId(), status, orderId);
		model.addAttribute("list",list);
		int count = orderinfoService.CountSta(status);
		model.addAttribute("count", count);
		model.addAttribute("status",status);
		return "mygxin.jsp";
	}
	//订单详情
		@RequestMapping("/toMyOrderDetail.do")
		public String toMyOrderDetail(String orderId,Model model) {
			//根据订单号查详情表
			List<Orderdetail> orderdetailList=orderinfoService.findOrderDetailByOrderId(orderId);
			//根据订单号查主表
			Orderinfo orderinfo=orderinfoService.findOrderinfoByOrderId(orderId);
			model.addAttribute("orderdetailList",orderdetailList);
			model.addAttribute("orderinfo",orderinfo);
			return "orderxq.jsp";
		}
		//确认收货
		@RequestMapping("/confirmSh.do")
		@ResponseBody
		public Map<String, Object> confirmSh(String orderId) {
			int num=orderinfoService.editOrderInfoSh(orderId);
			return num >0 ? LayUiResult.toClient("0", "确认收货成功") : LayUiResult.toClient("1", "确认收货失败");
		}
		//个人信息
				@RequestMapping("/toMygrxx.do")
				public String toMygrxx(HttpSession session,Model model) {
					//根据订单号查详情表
					User user = (User) session.getAttribute("loginUser");
					return "mygrxx.jsp";
				}
		
		
		
}